﻿using log4net;
using Nova_BLL;
using Nova_Common.Extend;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.RFSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.RFSystem.Controllers
{
    /// <summary>
    /// 手持盘点功能
    /// </summary>
    [RoutePrefix("api/RFSystem/CheckStorage")]
    [ControllerGroup("PDA手持", "盘点操作功能接口")]
    public class CheckStorageController : BaseApiController
    {
        RFBll RfBll = new RFBll();
        TCommandBll CommandBll = new TCommandBll();
        TBase_StorageBll Storagebll = new TBase_StorageBll();
        TSQModeRelationsBll SqModelRelationBll = new TSQModeRelationsBll();

        private ILog _logger = LogManager.GetLogger(typeof(CheckStorageController));

        /// <summary>
        /// 根据托盘码获取指令,物料等出库信息
        /// </summary>
        /// <param name="trayId">托盘码</param>
        /// <returns></returns>
        [HttpGet, Route("GetCheckCommandMatInfoByTrayId")]
        public ApiResult GetCheckCommandMatInfoByTrayId(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘码不能为空!");
            DataTable dt = RfBll.GetCheckCommandMatInfoByTrayId(trayId);
            List<TrayCheckStorageCommandModel> list = Common.DataTableToList<TrayCheckStorageCommandModel>(dt);
            var commandModel = CommandBll.GetByTrayId(trayId);
            if(commandModel.IsEmpty())
                return Error(ResultCode.ParaError, "当前盘点指令已完成!");
            var sqmodel = SqModelRelationBll.GetByWmsType(commandModel.TaskMode);
            return Success("根据托盘码获取盘点指令,物料等出库信息!",
                new
                {
                    TrayId = trayId,
                    PosS_Id = commandModel.PosS_ID,
                    TaskId = commandModel.TaskID,
                    TaskModelName = sqmodel.WMSTypeName,
                    MatList = list
                });
        }

        /// <summary>
        /// 盘点物料
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ApiResult CheckStorage(CheckStorageModel param)
        {
            if (param.Pid.IsEmpty())
                return Error(ResultCode.ParaError, "Pid不能为空!");
            if (param.MatNum < 0)
                return Error(ResultCode.ParaError, "数量不能小于0!");
            var commandModel = CommandBll.GetByTrayId(param.TrayId);
            if (commandModel.IsEmpty())
                return Error(ResultCode.ParaError, "盘点指令已完成，无需多次盘点!");
            var model = Storagebll.GetByPid(param.Pid);
            if (model.IsEmpty())
                return Error(ResultCode.ParaError, "当前物料库存不存在!");
            model.Mat_Num = param.MatNum;
            model.CDT = DateTime.Now;
            bool isUpdate = false;
            if (param.MatNum == 0)
                isUpdate = Storagebll.DeleteByPid(param.Pid);
            else
                isUpdate = Storagebll.UpdateEntity(model);
            return isUpdate ? Success("盘点物料数量成功!") : Error(ResultCode.OtherError, "盘点物料数量失败!");
        }

        /// <summary>
        /// 根据用户名和密码获取用户信息
        /// </summary>
        /// <param name="userNo"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet, Route("GetUserInfo")]
        public ApiResult GetUserInfo(string userNo, string password)
        {
            if (userNo.IsEmpty())
                return Error(ResultCode.ParaError, "userNo不能为空!");
            if (password.IsEmpty())
                return Error(ResultCode.ParaError, "password不能为空!");
            Nova_BLL.Users userBll = new Nova_BLL.Users();
            DataTable dt = userBll.GetByUserNoPassword(userNo, password);
            if (dt.Rows.Count > 0)
            {
                // 只有管理员权限的用户才可以盘点
                if (dt.Rows[0]["vGroupNo"].ToString() == "004")
                    return Success("");
                else
                    return Error(ResultCode.ParaError, "只有高级权限的用户才可以盘点!");
            }
            else
                return Error(ResultCode.ParaError, "用户名和密码错误!");
        }
    }
}
